package com.sgb.net.callback;

import android.accounts.NetworkErrorException;
import android.content.DialogInterface;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.sgb.net.entity.BaseEntity;
import com.sgb.net.pop.DialogHelper;
import com.sgb.net.util.CapitalLog;
import com.sgb.net.util.GetTopActivityUtil;
import com.sgb.net.util.MToast;
import com.sgb.net.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public abstract class ICallBack<T> implements Callback<T> {

    private boolean isAutoDismissDialog;

    public abstract void onSuccess(T var1);

    public void onFinally() {
        if (this.isAutoDismissDialog) {
            DialogHelper.dismissProgressDialog();
        }

    }

    public void onFail() {
    }

    public ICallBack() {
        this.isAutoDismissDialog = true;
    }

    public ICallBack(boolean isAutoDismissDialog) {
        this(true, isAutoDismissDialog, false);
    }

    public ICallBack(boolean isAutoDialog, boolean isAutoDismissDialog) {
        this(isAutoDialog, isAutoDismissDialog, false);
    }

    public ICallBack(boolean isAutoDialog, boolean isAutoDismissDialog, boolean isShowJump) {
        this.isAutoDismissDialog = true;
        this.init(isAutoDialog, isAutoDismissDialog, isShowJump);
    }

    private void init(boolean isAutoDialog, boolean isAutoDismissDialog, boolean isShowJump) {
        this.isAutoDismissDialog = isAutoDismissDialog;
        if (isAutoDialog && GetTopActivityUtil.getInstance().getTopActivity() != null && !DialogHelper.isShowing()) {
            DialogHelper.showProgressDialog(GetTopActivityUtil.getInstance().getTopActivity(), (String)null, "数据加载中...", 0, false, isShowJump, (DialogInterface.OnCancelListener)null).setCanceledOnTouchOutside(false);
        }

    }

    public void onResponse(Call<T> call, Response<T> response) {
        if ("release".equals("release")) {
            try {
                this.onResponseRelest(call, response);
            } catch (Exception var7) {
                this.onFailure(call, var7);
            } finally {
                this.onFinally();
            }
        } else {
            this.onResponseRelest(call, response);
            this.onFinally();
        }

    }

    private void onResponseRelest(Call<T> call, Response<T> response) {
        String message = "";
        if (response.isSuccessful() && response.body() != null) {
            String url = response.raw().request().url().toString();
            BaseEntity body = (BaseEntity)response.body();
            CapitalLog.e("api", (new Gson()).toJson(body));
            if (body.getState().equals("ok")) {
                this.onSuccess(response.body());
                return;
            }

            CapitalLog.e("api", "onFailure: 接口响应失败  url = " + url);
            message = this.onFailureWithCode(body);
            this.onFailureWithEntity(body);
        } else {
            CapitalLog.e("api", "onFailure: 接口响应失败  url = " + call.request().url().toString() + "body :  " + response.toString());
            message = "数据加载异常";
        }

        this.onFailure(call, new IOException(message));
    }

    public void onFailure(Call<T> call, Throwable t) {
        if ("release".equals("release")) {
            try {
                this.putException(call, t);
            } catch (Exception var9) {
                CapitalLog.e("接口解析错误，请检查接口数据格式和解析方式", var9.toString());
            } finally {
                this.onFail();
                this.onFinally();
            }
        } else {
            try {
                this.putException(call, t);
                this.onFail();
                this.onFinally();
            } catch (Exception var8) {
            }
        }

    }

    private void putException(Call<T> call, Throwable t) {
        String message = "";
        if (!(t instanceof ConnectException) && !(t instanceof UnknownHostException)) {
            if (t instanceof NetworkErrorException) {
                message = "无网络，请检查网络连接";
            } else if (t instanceof SocketTimeoutException) {
                message = "网络连接超时，请稍后再试";
            } else if (t instanceof IOException) {
                if ("数据加载异常".equals(t.getMessage())) {
                    message = "数据加载异常";
                } else if ("无网络，请检查网络连接".equals(t.getMessage())) {
                    message = "无网络，请检查网络连接";
                } else if ("接口响应失败".equals(t.getMessage())) {
                    if ("release".equals("release")) {
                        this.cleanTokenInfo();
                        message = "登录失效，请重新登录";
                    } else {
                        message = "接口请求失败";
                    }
                } else if (!TextUtils.isEmpty(t.getMessage()) && (t.getMessage().contains("请重新登录") || t.getMessage().contains("不允许访问"))) {
                    this.cleanTokenInfo();
                    message = "登录失效，请重新登录";
                } else if (!TextUtils.isEmpty(t.getMessage())) {
                    message = t.getMessage();
                } else {
                    message = "网络链接不稳定，请稍后重试";
                }
            } else if (t instanceof JsonSyntaxException) {
                if ("release".equals("release")) {
                    message = "网络链接不稳定，请稍后重试";
                } else {
                    message = "请检查您的接口解析是否正确";
                }
            } else if (!(t instanceof IllegalStateException) && !(t instanceof NumberFormatException)) {
                if (!TextUtils.isEmpty(t.getMessage())) {
                    message = t.getMessage();
                } else {
                    message = "网络链接不稳定，请稍后重试";
                }
            } else {
                CapitalLog.e("api", "onFailure: " + t);
            }
        } else {
            message = "网络链接不稳定，请稍后重试";
        }

        CapitalLog.e("api", "url=" + call.request().url().toString() + " ----onFailure: " + t);
        if (!TextUtils.isEmpty(message)) {
            if ("release".equals("release") && (message.contains("For") || message.contains("for") || message.contains("input") || message.contains("string") || message.contains("接口请求失败") || message.contains("数据加载异常") || message.contains("推荐"))) {
                message = "";
            }
        } else {
            message = "网络链接不稳定，请稍后重试";
        }

        MToast.showToast(GetTopActivityUtil.getInstance().getTopActivity(), message);
    }

    public void onFailureWithEntity(BaseEntity entity) {
    }

    public String onFailureWithCode(BaseEntity entity) {
        String code = TextUtils.isEmpty(entity.code) ? "200" : entity.code;
        String message = TextUtils.isEmpty(entity.getMsg()) ? "" : entity.getMsg();
        byte var5 = -1;
        switch(code.hashCode()) {
            case 49586:
                if (code.equals("200")) {
                    var5 = 0;
                }
                break;
            case 52469:
                if (code.equals("500")) {
                    var5 = 2;
                }
                break;
            case 53622:
                if (code.equals("666")) {
                    var5 = 1;
                }
        }

        switch(var5) {
            case 0:
                if (TextUtils.isEmpty(entity.getMsg())) {
                    message = "接口响应失败";
                }
                break;
            case 1:
                this.cleanTokenInfo();
                message = entity.getMsg();
                break;
            case 2:
                message = this.onFailureSwitchErrCode(entity);
                break;
            default:
                message = entity.getMsg() != null && !entity.getMsg().equals("") ? entity.getMsg() : "接口响应失败";
        }

        message = TextUtils.isEmpty(message) ? "" : message;
        if (message.contains("401") || message.contains("403") || message.contains("500") || message.contains("凭证失效") || message.contains("身份失效") || message.contains("身份校验") || message.contains("凭证无效") || message.contains("身份验证失败") || message.contains("Missing") || message.contains("身份校验失败")) {
            message = "不允许访问";
        }

        return message;
    }

    protected String onFailureSwitchErrCode(BaseEntity entity) {
        String message = TextUtils.isEmpty(entity.getMsg()) ? "" : entity.getMsg();
        String errCode = TextUtils.isEmpty(entity.errCode) ? "" : entity.errCode;
        String code = "0";
        if (errCode.length() == 6) {
            code = errCode.substring(0, 1);
        }

        byte var6 = -1;
        switch(code.hashCode()) {
            case 49:
                if (code.equals("1")) {
                    var6 = 0;
                }
                break;
            case 50:
                if (code.equals("2")) {
                    var6 = 1;
                }
            case 51:
            default:
                break;
            case 52:
                if (code.equals("4")) {
                    var6 = 2;
                }
                break;
            case 53:
                if (code.equals("5")) {
                    var6 = 3;
                }
                break;
            case 54:
                if (code.equals("6")) {
                    var6 = 4;
                }
        }

        switch(var6) {
            case 4:
                this.cleanTokenInfo();
                message = entity.getMsg();
            case 0:
            case 1:
            case 2:
            case 3:
            default:
                return message;
        }
    }

    private void cleanTokenInfo() {
        SharedPreferenceUtil.getInstance().saveData("resourcePCToken", "");
        EventBus.getDefault().post("LoginOut");
    }
}
